<!DOCTYPE html>
<html lang="zh" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="fusion-docs">
<meta name="generator" content="Paradox, paradox-material-theme=0.6.0, mkdocs-material=3.0.3">

<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">


<meta name="description" content="fusion-docs">
<link rel="shortcut icon" href="../assets/images/favicon.png">
<title>HTTP 代理网关 · Akka Fusion</title>
<link rel="stylesheet" href="../assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="../assets/stylesheets/application-palette.22915126.css">
<meta name="theme-color" content="#3f51b5" />
<link rel="stylesheet" href="../lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="../lib/prettify/prettify.css">
<script src="../assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="../assets/fonts/font-awesome.css">
<link rel="stylesheet" href="../assets/fonts/material-icons.css">
<link rel="stylesheet" href="../assets/stylesheets/paradox-material-theme.css">
</head>
<body
data-md-color-primary="indigo"
data-md-color-accent="red"
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../index.html" title="Akka Fusion" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
Akka Fusion
</span>
<span class="md-header-nav__topic">
HTTP 代理网关
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">&#xE5CD;</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>

</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/akka-fusion/akka-fusion"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
akka-fusion/akka-fusion
</div>
</a>

</div>
</div>
</div>
</nav>
</header>

<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="../index.html" title="Akka Fusion" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="../index.html" title="Akka Fusion">
Akka Fusion
</a>
</label>
<div class="md-nav__source">
<a href="https://github.com/akka-fusion/akka-fusion"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
akka-fusion/akka-fusion
</div>
</a>

</div>
<ul>
  <li><a href="../intro/index.html" class="page">简介</a></li>
  <li><a href="../core/index.html" class="page">核心</a>
  <ul>
    <li><a href="../core/akka-extension.html" class="page">Akka Extension（扩展）</a></li>
    <li><a href="../core/http-route.html" class="page">Http Route（异步处理器）</a></li>
    <li><a href="../core/fusion-core.html" class="page">FusionCore</a></li>
  </ul></li>
  <li><a href="../http/index.html" class="page">HTTP</a>
  <ul>
    <li><a href="../http/getting.html" class="page">开始</a></li>
    <li><a href="../http/management.html" class="page">管理API</a></li>
    <li><a href="../http/interceptor-chain.html" class="page">Interceptor Chain（拦截器链）</a></li>
  </ul></li>
  <li><a href="../discovery-client/index.html" class="page">服务注册、发现（Client）</a>
  <ul>
    <li><a href="../discovery-client/nacos.html" class="page">连接 Nacos</a></li>
  </ul></li>
  <li><a href="../http-gateway/index.html" class="active page">HTTP 代理网关</a></li>
  <li><a href="../jdbc/index.html" class="page">Jdbc</a></li>
  <li><a href="../slick/index.html" class="page">Slick</a></li>
  <li><a href="../mybatis/index.html" class="page">MyBatis</a>
  <ul>
    <li><a href="../mybatis/getting.html" class="page">起步</a></li>
    <li><a href="../mybatis/configuration.html" class="page">配置</a></li>
  </ul></li>
  <li><a href="../data-mongodb/index.html" class="page">Data Mongodb</a></li>
  <li><a href="../data-kafka/index.html" class="page">Data Kafka</a>
  <ul>
    <li><a href="../data-kafka/getting.html" class="page">开始</a></li>
    <li><a href="../data-kafka/producer.html" class="page">Kafka Producer</a></li>
    <li><a href="../data-kafka/consumer.html" class="page">Kafka Consumer</a></li>
  </ul></li>
  <li><a href="../inject/index.html" class="page">依赖管理</a>
  <ul>
    <li><a href="../inject/guice.html" class="page">使用 Guice 进行类依赖管理</a></li>
  </ul></li>
  <li><a href="../log/index.html" class="page">日志</a></li>
  <li><a href="../configuration/index.html" class="page">配置</a>
  <ul>
    <li><a href="../configuration/core.html" class="page">Fusion Core 配置</a></li>
    <li><a href="../configuration/http.html" class="page">Fusion HTTP 配置</a></li>
    <li><a href="../configuration/http-gateway.html" class="page">Fusion HTTP Gateway 配置</a></li>
    <li><a href="../configuration/jdbc.html" class="page">Fusion JDBC 配置</a></li>
    <li><a href="../configuration/slick.html" class="page">Fusion Slick 配置</a></li>
    <li><a href="../configuration/mybatis.html" class="page">Fusion Mybatis 配置</a></li>
    <li><a href="../configuration/mongodb.html" class="page">Fusion Mongodb 配置</a></li>
    <li><a href="../configuration/kafka.html" class="page">Fusion Kafka 配置</a></li>
  </ul></li>
  <li><a href="../reactivemanifesto.html" class="page">反应式宣言 2.0</a></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../http-gateway/index.html#http-代理网关" class="header">HTTP 代理网关</a>
  <ul>
    <li><a href="../http-gateway/index.html#特性" class="header">特性</a></li>
    <li><a href="../http-gateway/index.html#配置实现" class="header">配置实现</a></li>
    <li><a href="../http-gateway/index.html#编程实现" class="header">编程实现</a></li>
    <li><a href="../http-gateway/index.html#自定义-httpgatewaycomponent" class="header">自定义 <code>HttpGatewayComponent</code></a></li>
  </ul></li>
</ul>
</nav>

</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 2.0.6
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../http-gateway/index.html#http-代理网关" class="header">HTTP 代理网关</a>
  <ul>
    <li><a href="../http-gateway/index.html#特性" class="header">特性</a></li>
    <li><a href="../http-gateway/index.html#配置实现" class="header">配置实现</a></li>
    <li><a href="../http-gateway/index.html#编程实现" class="header">编程实现</a></li>
    <li><a href="../http-gateway/index.html#自定义-httpgatewaycomponent" class="header">自定义 <code>HttpGatewayComponent</code></a></li>
  </ul></li>
</ul>
</nav>

</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#http-代理网关" name="http-代理网关" class="anchor"><span class="anchor-link"></span></a>HTTP 代理网关</h1>
<p><strong>fusion-http-gateway</strong></p>
<p>fusion-http-gateway基于 Akka HTTP 实现了一个强大的API网关代理功能，可通过 Lightbend Config 进行配置或编码实现。fusion-http-gateway支持对HTTP 1.0、1.1和2.0请求进行代理，同时还支持GRPC（使用HTTP 2实现）。</p>
<h2><a href="#特性" name="特性" class="anchor"><span class="anchor-link"></span></a>特性</h2>
<ul>
  <li>支持HTTP 1.0、1.1、2.0</li>
  <li>支持GRPC</li>
  <li>支持服务发现（通过 Akka Discovery）</li>
  <li>支持自定义代理策略（如：修改上传body大小限制、请求超时等）</li>
  <li>支持HTTP拦截器</li>
  <li>支持熔断（通过 Akka CircuitBreaker 对某一地址设置熔断策略）</li>
</ul>
<p>最后，若你想要更大的可控性，可以继承 <code>HttpGatewayComponent</code> 实现你自己的HTTP代理组件。</p>
<h2><a href="#配置实现" name="配置实现" class="anchor"><span class="anchor-link"></span></a>配置实现</h2>
<p>fusion-http-gateway的配置主要分两部分（参考了Nginx）：</p>
<ol>
  <li><code>upstreams</code>：设置上游服务</li>
  <li><code>locations</code>：指定需要代理的地址</li>
</ol>
<h4><a href="#upstream" name="upstream" class="anchor"><span class="anchor-link"></span></a>upstream</h4>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-http-gateway/src/main/resources/template.conf#L26-L41" target="_blank" title="Go to snippet source"></a><code class="language-conf">// 服务上游配置
upstreams {
  # 上游服务名
  account {
    # 动态服务发现配置
    # 服务发现服务名字，将通过 Akka Discovery 发现服务实际地址
    serviceName = fusion-server-account
    # 默认使用 akka.discovery.method 指定的 DiscoveryService
    #discoveryMethod = nacos
  }
  file-app {
    # 静态服务发现配置
    # 数组参数，可指定多个服务目标地址
    targets = [&quot;10.0.0.7:8083&quot;, &quot;10.0.0.8:8083&quot;]
  }
}</code></pre>
<h4><a href="#location" name="location" class="anchor"><span class="anchor-link"></span></a>location</h4>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-http-gateway/src/main/resources/template.conf#L45-L60" target="_blank" title="Go to snippet source"></a><code class="language-conf">// 代理地址
locations {
  &quot;/api/v4/account&quot; {
    # 上游服务名
    upstream = account
    # 代理转发地址，未设置同 /api/v4/account
    proxy-to = &quot;/api/v4/account&quot;
  }
  &quot;/file/akka-fusion_files&quot; {
    upstream = file-app
    routing-settings {
      # 设置上传文件不限制大小
      size-limit = -1
    }
  }
}</code></pre>
<h4><a href="#完整配置" name="完整配置" class="anchor"><span class="anchor-link"></span></a>完整配置</h4>
<p>详细配置见： <a href="../configuration/http-gateway.html">Fusion Http Gateway 配置</a>。</p>
<h2><a href="#编程实现" name="编程实现" class="anchor"><span class="anchor-link"></span></a>编程实现</h2>
<p>编程实现非常简单，使用常规的Akka HTTP功能即可。对Akka HTTP不熟悉的用户可阅读一本还不错的电子书：<a href="https://www.yangbajing.me/scala-web-development/">《Scala Web 开发——基于Akka HTTP》</a>。</p>
<h2><a href="#自定义-httpgatewaycomponent" name="自定义-httpgatewaycomponent" class="anchor"><span class="anchor-link"></span></a>自定义 <code>HttpGatewayComponent</code></h2>
<p>akka fusion提供了 <code>HttpGatewayComponent</code> 抽象类以使用户可以完全自定义HTTP代理。只需要继承 <code>HttpGatewayComponent</code> ，并将类的完整限定名配置到 <code>fusion.http.&lt;default&gt;.gateway.class</code> 属性即可。akka fusion使用用户提供的类来实例化HTTP代理组件。下面是一个示例：</p>
<p><strong>配置</strong></p>
<pre class="prettyprint"><code class="language-hocon">fusion.http.default.gateway {
  class = fusion.docs.gateway.CustomHttpGatewayComponent
}
</code></pre>
<p><strong>自定义HTTP网关代理组件</strong></p>
<pre class="prettyprint"><code class="language-scala">package fusion.docs.gateway

final class CustomHttpGatewayComponent(id: String, system: ActorSystem) 
    extends HttpGatewayComponent 
    with SessionDirectives {
  private val aggregate = BackendAggregate(system)

  override def route: Route = {
    validationSession(aggregate.sessionComponent) {                        // 校验session
      logging(aggregate.sessionComponent, 
          logMsg =&gt; aggregate.kafkaMessageService.processLogMsg(logMsg)) { // 记录日志
        super.route  
      }
    }
  }
}
</code></pre>
<p>这里我们使用 <code>validationSession</code> Directive来校验请求是否包含了有效的用户登录会话信息，<code>logging</code>指定记录HttpRequest、HttpResponse日志；可以看到，这里将请求、响应日志发送到了 Kafka 中，再由其它服务从 Kafka 里消费日志以对其进行进一步处理。</p>
</div>
<div>
<a href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-docs/src/main/paradox/http-gateway/index.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
2.0.6
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../discovery-client/nacos.html" title="连接 Nacos" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
连接 Nacos
</span>
</div>
</a>
<a href="../jdbc/index.html" title="Jdbc" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
Jdbc
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>

</div>
<div class="md-footer-social">
<a href="https://akka-fusion.github.io/akka-fusion/" class="md-footer-social__link fa fa-globe"></a><a href="https://github.com/akka-fusion" class="md-footer-social__link fa fa-github"></a><a href="https://weibo.com/yangbajing" class="md-footer-social__link fa fa-globe"></a>
</div>

</div>
</div>
</footer>

</div>
<script src="../assets/javascripts/application.583bbe55.js"></script>
<script src="../assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"../."}})</script>
<script type="text/javascript" src="../lib/prettify/prettify.js"></script>
<script type="text/javascript" src="../lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>
